package com.guigu.zj.controller;

import com.guigu.pojo.Mealgroup;
import com.guigu.pojo.Setmeal;
import com.guigu.pojo.Ychecknape;
import com.guigu.zj.pojo.PageMeal;
import com.guigu.zj.pojo.PageVo;
import com.guigu.zj.service.MealGroupService;
import com.guigu.zj.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class MealController {
    @Autowired
    MealService mealService;
    @Autowired
    MealGroupService mealGroupService;
    @RequestMapping("/Z_queryMeal")
    public PageVo<Setmeal> queryMeal(@RequestBody PageVo<Setmeal> pageVo){
        return mealService.queryAll(pageVo);
    }

    @RequestMapping("Z_addMeal")
    public  int addMeal(@RequestBody PageMeal pageMeal, @RequestParam("file") MultipartFile file) throws IOException {
        String newFile = UUID.randomUUID().toString();
        String orig = file.getOriginalFilename();
        String substring = orig.substring(orig.lastIndexOf("."));
        File file1 = new File("D:\\images\\");
        if(!file1.exists()){
            file1.mkdir();
        }
        file.transferTo(new File("D:/images"+newFile+substring));
        pageMeal.getSetmeal().setImgmeal(newFile);


        int i = mealService.insertSelective(pageMeal.getSetmeal());
        Integer id = pageMeal.getSetmeal().getId();
        int sum=0;
        for (Ychecknape mpri:pageMeal.getChe()) {
            Mealgroup mealgroup = new Mealgroup();
            mealgroup.setItemid(mpri.getId());
            mealgroup.setMealid(id);
            sum+=mealService.insertSelective(mealgroup);
        }
        return sum;
    }

    @RequestMapping("/Z_mealById")
    public Setmeal mealById(@RequestBody Setmeal setmeal){

        return mealService.selectByPrimaryKey(setmeal.getId());
    }
    @RequestMapping("Z_selectBy")
    public List<Mealgroup> selectBy(@RequestBody Mealgroup mealgroup){
        return mealService.selectBy(mealgroup.getId());
    }

    @RequestMapping("/Z_MealUp")
    public int MealUp(@RequestBody PageMeal pageMeal, MultipartFile file){

        int i = mealService.updateByPrimaryKeySelective(pageMeal.getSetmeal());
        Integer id = pageMeal.getSetmeal().getId();
        int sum=0;
        for (Ychecknape mpri:pageMeal.getChe()) {
            Mealgroup mealgroup = new Mealgroup();
            mealgroup.setItemid(mpri.getId());
            mealgroup.setMealid(id);
            sum+=mealService.insertSelective(mealgroup);
        }
        return sum;
    }

    @RequestMapping("Z_delMeal")
    public int delMeal(@RequestBody Setmeal setmeal){
        return mealService.deleteByPrimaryKey(setmeal.getId());

    }



}
